
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cassert>
using std::cout;
using std::endl;

namespace ddsm
{
    template<class T>
    class vector
    {   
    public:
        using iterator = T*;
        using const_iterator = const T*;
        //默认构造
        vector()
            :_start(nullptr)
            ,_finish(nullptr)
            ,_end_of_storage(nullptr)
        {}
        //初始化列初始化
        //把外部迭代器指向的对象一个一个push进入vector
        template<class InputIterator>
        vector(const InputIterator& begin,const InputIterator& end)
        {
            reserve(end - begin);
            InputIterator it = begin;
            while(it != end)
            {
                push_back(*it);
                ++it;
            }
        }
        void push_back(const T& val)
        {
            iterator pos = _start + size();//插入位置
            insert(pos,val);
        }
        //只扩容不缩容
        void reserve(int n)
        {
            //保存旧size,防止指针位置改变导致oldsize无法计算
            int oldsize = size();
            if(n > capacity())
            {//需要扩容
                iterator tem = new T[n];
                if(_start)//原指针不为空，有数据
                {
                    //需要实现深拷贝，调用对应的拷贝构造，不能memmove浅拷贝
                    for(int i = 0;i < oldsize;++i)
                    {
                        *(tem + i) = *(_start + i);
                    }
                }
                if(_start)
                    delete[] _start;
                _start = tem;
                _finish = _start + oldsize;
                _end_of_storage = _start + n;
            }
        }
        void swap(vector<T>& v)
        {
            std::swap(_start,v._start);
            std::swap(_finish,v._finish);
            std::swap(_end_of_storage,v._end_of_storage);
        }
        //拷贝构造
        vector(const vector<T>& vec)
        {
            reserve(vec.size());
            for(auto e : vec)
            {
                push_back(e);
            }
        }
        //在pos位置插入对象
		iterator insert(iterator pos, const T& t)
        {
            int len = pos - _start;
            if(size() == capacity())
            {//需要扩容
                int NewCapacity = capacity() == 0 ? 4 : capacity() * 2;
                reserve(NewCapacity);
                pos = _start + len;//修正pos指向的位置，pos原来指向的位置可能已经被释放，进而pos成为野指针
            }
            //插入逻辑插入一个数，后面的数依次向后移动
            iterator end = _finish;
            while(end != pos)
            {
                *end = *(end - 1);
                --end;
            }
            *pos = t;
            return pos;
        }
        //一般不会出现迭代器失效的问题--因为没有new新空间
		iterator erase(iterator pos)
        {//删除pos位置的数据
            iterator oldpos = pos;
            iterator start = pos + 1;
            // while(start != _finish-1)
            // {
            //     *(start) = *(start + 1);
            //     ++start;
            // }
            while (start < _finish)
			{
				*(start - 1) = *start;
				++start;
			}
            --_finish;
            return oldpos;
        }
        size_t capacity() const
        {
            return _end_of_storage - _start;
        }
        size_t capacity()
        {
            return _end_of_storage - _start;
        }
        size_t size() const
        {
            return _finish - _start;
        }
        size_t size() 
        {
            return _finish - _start;
        }
        T& operator[](int pos)
        {
            assert(pos >= 0 && pos < size());
            return _start[pos];
        }
        const T& operator[](int pos) const
        {
            assert(pos >= 0 && pos < size());
            return _start[pos];
        }
        iterator begin()
        {
            return _start;
        }
        iterator begin() const
        {
            return _start;
        }
        ~vector()
        {
            if(_start)
                delete[] _start;
            _finish = _end_of_storage = nullptr;
        }
    private:
        iterator _start;
        iterator _finish;
        iterator _end_of_storage;
    };

    
    std::ostream& operator<<(std::ostream& out,const vector<int>& v)
    {
        int size = v.size();
        for(int i = 0;i < size;++i)
        {
            cout<<v[i]<<" ";
        }
        cout<<endl;
        return out;
    }
};

int main()
{
    std::vector<int> array = {1,2,3,4,5};
    int len = sizeof(array)/sizeof(int);
    ddsm::vector<int> v1(array.begin(),array.end());
    cout<<v1<<endl;

    v1.erase(v1.begin());
    cout<<v1<<endl;
    return 0;
}